package com.summer.mall.controller;

import com.github.pagehelper.PageInfo;
import com.summer.mall.common.ResponseService;
import com.summer.mall.service.ProductService;
import com.summer.mall.vo.ProductDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/detail",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService<ProductDetailVo> getDetail(Integer productId){
        return productService.productDetail(productId);
    }

    /**
     * 产品详情 RESTful
     */
    //http://localhost:8080/product/100002
    @RequestMapping(value = "/{productId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<ProductDetailVo> getProductListRESTful(@PathVariable Integer productId){
        return productService.productDetail(productId);
    }


    @RequestMapping("/test")
    @ResponseBody
    public ResponseService getProduct(Integer productId){
        return productService.testFindProduct(productId);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService getProductListByCategoryIdAndKeyword(@RequestParam(value="categoryId",required = false) Integer categoryId,
                                                                @RequestParam(value="keyword",required = false) String keyword,
                                                                @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize,
                                                                @RequestParam(value="orderBy",defaultValue = "") String orderBy){
        return productService.getProductByKeywordCategoryId(categoryId,null,pageNum,pageSize,orderBy);
    }

    //http://localhost:8080/product/100002/1/10/price_asc/
    @RequestMapping(value = "/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<PageInfo> getProductListRESTful(@PathVariable(value = "categoryId")Integer categoryId,
                                                          @PathVariable(value = "pageNum") Integer pageNum,
                                                          @PathVariable(value = "pageSize") Integer pageSize,
                                                          @PathVariable(value = "orderBy") String orderBy) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if (StringUtils.isBlank(orderBy)) {
            orderBy = "price_asc";
        }
        return productService.getProductByCategoryId(categoryId,pageNum,pageSize,orderBy);
    }

    //http://localhost:8080/product/keyword/手机/1/10/price_asc/
    @RequestMapping(value = "/keyword/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<PageInfo> listRESTful(@RequestParam(value = "keyword") String keyword,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                @PathVariable(value = "orderBy") String orderBy){
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(StringUtils.isBlank(orderBy)){
            orderBy = "price_asc";
        }

        return productService.getProductByKeywordCategoryId(null,keyword,pageNum,pageSize,orderBy);
    }

    //http://localhost:8080/product/category/100012/1/10/price_asc/
    @RequestMapping(value = "/category/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseService<PageInfo> listRESTful(@PathVariable(value = "categoryId")Integer categoryId,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                @PathVariable(value = "orderBy") String orderBy){
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(StringUtils.isBlank(orderBy)){
            orderBy = "price_asc";
        }

        return productService.getProductByKeywordCategoryId(categoryId,null,pageNum,pageSize,orderBy);
    }
}
